//
//  LJFCustomController.m
//  SevenZhou
//
//  Created by qianfeng on 15/9/14.
//  Copyright (c) 2015年 ljf. All rights reserved.
//

#import "LJFCustomController.h"
#import "Masonry.h"

@interface LJFCustomController ()
{
    NSInteger _index;
}

@end

@implementation LJFCustomController

- (void)viewDidLoad
{
    
    [super viewDidLoad];

    
    [self LoadCustomTabBar];
    
}

- (void)LoadCustomTabBar
{
    self.tabBar.hidden = YES;
    
    _tabBarView = [[UIImageView alloc] init];
    _tabBarView.image = [UIImage imageNamed:@"footer_bg"];
    _tabBarView.userInteractionEnabled = YES;
    [self.view addSubview:_tabBarView];
    
    [_tabBarView mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(0);
        make.height.equalTo(@49);
        
    }];
    
    [self AddButtonToTaBar];
    
}

- (void)AddButtonToTaBar
{
    NSArray *nomal = @[@"footer_home_icon",@"footer_search_icon",@"dest_icon_unselect",@"icon_activity_gray",@"footer_user_icon"];
    NSArray *select = @[@"footer_home_active_icon",@"footer_search_active_icon",@"dest_icon_select",@"icon_activity_green",@"footer_user_active_icon"];
    
    CGFloat MaginW = 35;
    CGFloat Magin = (self.view.frame.size.width-205)/4;
    
    for (int i=0 ; i<5; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [button setBackgroundImage:[UIImage imageNamed:nomal[i]] forState:UIControlStateNormal];
        
        [button setBackgroundImage:[UIImage imageNamed:select[i]] forState:UIControlStateSelected];
        button.tag = 100 + i;
        [button addTarget:self action:@selector(selectButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [_tabBarView addSubview:button];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make)
         {
           
             make.left.equalTo(_tabBarView.mas_left).offset(i*(MaginW+Magin)+15);
             make.bottom.equalTo(_tabBarView.mas_bottom).offset(-7);
             make.width.equalTo(@35);
             make.height.equalTo(@35);
             
         }];
    }
    
    UIButton *button = (UIButton *)[_tabBarView viewWithTag:100];
    button.selected = YES;
}

- (void)selectButton:(UIButton *)button;
{
    for (int i=0; i<5; i++)
    {
        UIButton *b = (UIButton *)[_tabBarView viewWithTag:100+i];
        b.selected = NO;
    }
    
    button.selected = YES;
    
    CATransition* animation = [CATransition animation];
    [animation setDuration:0.5f];
//    [animation setType:@"cube"];
    [animation setType:kCATransitionPush];
    if (_index < button.tag - 100)
    {
        [animation setSubtype:kCATransitionFromRight];
    }
    else
    {
        [animation setSubtype:kCATransitionFromLeft];
    }
    
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
    [self.view.layer addAnimation:animation forKey:nil];
    
    self.selectedIndex = button.tag - 100;
    
    _index = button.tag - 100;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)hiddenTabar
{
    self.tabBarView.hidden = YES;
}
- (void)showTabar
{
    self.tabBarView.hidden = NO;
}

+ (void)tabarControllerhiddenTabar
{
    LJFCustomController *tabar = [[self alloc] init];
    
    [tabar hiddenTabar];
}

+ (void)tabarControllershowTabar
{
    LJFCustomController *tabar = [[self alloc] init];
    
    [tabar showTabar];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
